package com.atguigu.eduService.service.impl;

import com.atguigu.baseservice.handler.GuliException;
import com.atguigu.eduService.client.VodClient;
import com.atguigu.eduService.entity.EduChapter;
import com.atguigu.eduService.entity.EduCourse;
import com.atguigu.eduService.entity.EduCourseDescription;
import com.atguigu.eduService.entity.EduVideo;
import com.atguigu.eduService.entity.vo.CourseInfoForm;
import com.atguigu.eduService.entity.vo.CoursePublishVo;
import com.atguigu.eduService.entity.vo.CourseQueryVo;
import com.atguigu.eduService.entity.vo.CourseWebVo;
import com.atguigu.eduService.mapper.EduCourseMapper;
import com.atguigu.eduService.service.EduChapterService;
import com.atguigu.eduService.service.EduCourseDescriptionService;
import com.atguigu.eduService.service.EduCourseService;
import com.atguigu.eduService.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.*;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-11-15
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {
    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private VodClient vodClient;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduVideoService eduVideoService;

    //添加课程信息
    @Override
    public String addCourseInfo(CourseInfoForm courseInfoForm) {
        //1.添加课程信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new GuliException(20001, "插入失败");

        }

        //2.获取课程id
        String courseId = eduCourse.getId();

        //3.添加课程描述信息
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setId(courseId);
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescriptionService.save(courseDescription);
        return courseId;
    }

    @Override
    public CourseInfoForm getCourseInfoById(String id) {
        EduCourse eduCourse = baseMapper.selectById(id);
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        BeanUtils.copyProperties(eduCourse, courseInfoForm);
        EduCourseDescription courseDescription = courseDescriptionService.getById(id);
        courseInfoForm.setDescription(courseDescription.getDescription());

        return courseInfoForm;
    }

    //修改课程信息
    @Override
    public void updateCourseInfo(CourseInfoForm courseInfoForm) {
        //1.复制课程数据
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm, eduCourse);
        //2.更新课程数据
        int update = baseMapper.updateById(eduCourse);
        //3.判断是否成功
        if (update == 0) {
            throw new GuliException(20001, "更新失败");
        }
        //4.跟新课程描述
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoForm.getDescription());
        courseDescription.setId(courseInfoForm.getId());
        courseDescriptionService.getById(courseDescription);


    }

    @Override
    public CoursePublishVo getCoursePublishById(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getCoursePublishById(id);
        return coursePublishVo;
    }

    @Override
    public void delCourseInfo(String id) {
        QueryWrapper<EduVideo> videoIdsWrapper = new QueryWrapper();
        videoIdsWrapper.eq("id", id);
        List<EduVideo> videoIdsList = eduVideoService.list(videoIdsWrapper);
        List<String> videoIds = new ArrayList<>();
        for (int i = 0; i < videoIdsList.size(); i++) {
            String videoId = videoIdsList.get(i).getVideoSourceId();
            videoIds.add(videoId);
        }
        if (videoIds.size() > 0) {
            vodClient.delVideoList(videoIds);
        }


        QueryWrapper<EduVideo> videoWrapper = new QueryWrapper<>();
        videoWrapper.eq("course_id", id);
        eduVideoService.remove(videoWrapper);

        QueryWrapper<EduChapter> chapterWrapper = new QueryWrapper<>();
        chapterWrapper.eq("course_id", id);
        eduChapterService.remove(chapterWrapper);
        courseDescriptionService.removeById(id);
        int i = baseMapper.deleteById(id);
        if (i == 0) {
            throw new GuliException(20001, "删除课程失败");
        }

    }

    @Override
    public Map<String, Object> getCourseApiPageVo(Page<EduCourse> page, CourseQueryVo courseQueryVo) {
        String parentId = courseQueryVo.getSubjectParentId();
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(parentId)) {
            queryWrapper.eq("subject_parent_id", parentId);
        }
        IPage<EduCourse> param = baseMapper.selectPage(page, queryWrapper);
        long current = param.getCurrent();
        Map<String, Object> map = new HashMap<>();
        map.put("current", current);
        return map;
    }

    @Override
    public CourseWebVo getCourseWebVo(String id) {
        CourseWebVo courseWebVo = baseMapper.getCourseWebVo(id);
        return courseWebVo;
    }
}
